package com.binguo.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author yuritian
 * @Title: UploadFileService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2020/6/614:11
 */
@Service
public class UploadFileService {

    public String uploadImg(Integer shopId,
                            Integer type,
                            String base64,
                            String extName){
        JSONObject param = new JSONObject();
        param.put("shopId", shopId);
        if (type == 1) {
            param.put("bucketName", "face");
        }else if (type == 2){
            param.put("bucketName", "posproduct");
        }else {
            param.put("bucketName", "normal");
        }
        param.put("base64", base64);
        param.put("suffix", extName);
        param.put("fileName",String.valueOf(System.currentTimeMillis()));

        HttpHeaders DEFAULT_JSON_HEADER = new HttpHeaders();
        MediaType jsontype = MediaType.APPLICATION_JSON;
        DEFAULT_JSON_HEADER.setContentType(jsontype);
        DEFAULT_JSON_HEADER.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> entity = new HttpEntity<String>(param.toJSONString(), DEFAULT_JSON_HEADER);

        //ResponseEntity<String> resp = new RestTemplate().postForEntity(IpAddressUtil.ipAddress + "/api/local/cos/uploadImage", entity, String.class);
        //ResponseEntity<String> resp = new RestTemplate().postForEntity( "http://192.168.10.27/api/local/cos/uploadImage", entity, String.class);
        ResponseEntity<String> resp = new RestTemplate().postForEntity( "https://tc.yunpengai.com/upload/api/minio/uploadFileByCustomer", entity, String.class);


        String createResult = resp.getBody();
        JSONObject json = JSONObject.parseObject(createResult);
        Integer code = json.getInteger("code");
        if (code.equals(200)) {
            String picUrl =  json.getString("data");
            return picUrl;
        } else {
            return null;
        }
    }
}
